#include "phoneloginwidget.h"

PhoneLoginWidget::PhoneLoginWidget(QWidget *parent) : QWidget{parent}
{
    // 1. 设置窗口基本属性
    this->setFixedSize(400, 350);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建核心布局管理器
    QGridLayout *layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50, 0, 50, 0);
    this->setLayout(layout);

    // 3. 创建标题
    _titleLabel = new QLabel();
    _titleLabel->setText("登录");
    _titleLabel->setFixedHeight(50);
    _titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _titleLabel->setStyleSheet("QLabel { font-size: 40px; font-weight: 600; }");
    _titleLabel->setAlignment(Qt::AlignCenter);

    // 4. 创建手机输入框
    QString editStyle =
        "QLineEdit { border: none; background-color: rgb(240, 240, 240); font-size: 20px; border-radius: "
        "10px; padding-left: 5px;}";
    _phoneEdit = new QLineEdit();
    _phoneEdit->setPlaceholderText("输入手机号");
    _phoneEdit->setFixedHeight(40);
    _phoneEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _phoneEdit->setStyleSheet(editStyle);

    // 5. 创建验证码输入框
    _verifyCodeEidt = new QLineEdit();
    _verifyCodeEidt->setPlaceholderText("输入短信验证码");
    _verifyCodeEidt->setFixedHeight(40);
    _verifyCodeEidt->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _verifyCodeEidt->setStyleSheet(editStyle);

    // 6. 创建发送验证码按钮
    QString btnWhiteStyle = "QPushButton { border: none; border-radius: 10px; background-color: transparent; }";
    btnWhiteStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    _sendVerifyCodeBtn = new QPushButton();
    _sendVerifyCodeBtn->setFixedSize(100, 40);
    _sendVerifyCodeBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _sendVerifyCodeBtn->setText("发送验证码");
    _sendVerifyCodeBtn->setStyleSheet(btnWhiteStyle);

    // 7. 创建提交按钮
    _submitBtn = new QPushButton();
    _submitBtn->setFixedHeight(40);
    _submitBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _submitBtn->setText("登录");
    QString btnGreenStyle = "QPushButton { border: none; border-radius: 10px; background-color: rgb(44, 182, 61); "
                            "color: rgb(255, 255, 255); }";
    btnGreenStyle += "QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    _submitBtn->setStyleSheet(btnGreenStyle);

    // 8. 创建 “切换到用户名” 模式按钮
    QPushButton *userModeBtn = new QPushButton();
    userModeBtn->setFixedSize(100, 40);
    userModeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    userModeBtn->setText("切换到用户名");
    userModeBtn->setStyleSheet(btnWhiteStyle);

    // 9. 切换登录注册样式
    _switchModeBtn = new QPushButton();
    _switchModeBtn->setFixedSize(100, 40);
    _switchModeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    _switchModeBtn->setText("注册");
    _switchModeBtn->setStyleSheet(btnWhiteStyle);

    // 10. 添加到布局管理器
    layout->addWidget(_titleLabel, 0, 0, 1, 5);
    layout->addWidget(_phoneEdit, 1, 0, 1, 5);
    layout->addWidget(_verifyCodeEidt, 2, 0, 1, 4);
    layout->addWidget(_sendVerifyCodeBtn, 2, 4, 1, 1);
    layout->addWidget(_submitBtn, 3, 0, 1, 5);
    layout->addWidget(userModeBtn, 4, 0, 1, 1);
    layout->addWidget(_switchModeBtn, 4, 4, 1, 1);

    // 11. 添加信号槽
    connect(_switchModeBtn, &QPushButton::clicked, this, &PhoneLoginWidget::switchMode);
    connect(userModeBtn, &QPushButton::clicked, this,
            [=]()
            {
                LoginWidget *loginWidget = new LoginWidget(nullptr);
                loginWidget->show();
                this->close();
            });
}

void PhoneLoginWidget::switchMode()
{
    if (_isLoginMode)
    {
        // 切换到注册模式
        this->setWindowTitle("注册");
        _titleLabel->setText("注册");
        _submitBtn->setText("注册");
        _switchModeBtn->setText("登录");
    }
    else
    {
        // 切换到登录模式
        this->setWindowTitle("登录");
        _titleLabel->setText("登录");
        _submitBtn->setText("登录");
        _switchModeBtn->setText("注册");
    }
    _isLoginMode = !_isLoginMode;
}
